package usercollect

import (
	"context"
	"strings"
	"time"

	"mall/common/globalkey"
	"mall/common/modelx"
	"mall/common/resultx"
	"mall/service/backend/common/i18n"
	"mall/service/forum/model"
	"mall/service/forum/rpc/internal/svc"
	"mall/service/forum/rpc/internal/utils/errorhandler"
	"mall/service/forum/rpc/types/forum"

	"github.com/zeromicro/go-zero/core/logx"
)

type DeleteUserCollectLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewDeleteUserCollectLogic(ctx context.Context, svcCtx *svc.ServiceContext) *DeleteUserCollectLogic {
	return &DeleteUserCollectLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

func (l *DeleteUserCollectLogic) DeleteUserCollect(in *forum.RemoveUserCollectReq) (*forum.BaseResp, error) {
	if in.VisitorId == nil || *in.VisitorId == 0 {
		return nil, resultx.StatusError(resultx.USER_ID_REQUIRED, "")
	}

	userId := *in.VisitorId
	if in.Clear != nil {
		filterMap := make(map[string]map[string][]any)
		filterMap[model.ForumUserCollectColumns.UserID] = map[string][]any{
			"equalTo": []any{userId},
		}
		filterMap[model.ForumUserCollectColumns.Status] = map[string][]any{
			"equalTo": []any{globalkey.StatusEnabled},
		}
		list, _, err := l.svcCtx.UserCollect.GetList(l.ctx, model.M{}, -1, filterMap)
		if err != nil {
			return nil, errorhandler.DefaultRepositoryError(l.Logger, err, in)
		}

		if len(list) > 0 {
			var ids []uint64
			for _, v := range list {
				ids = append(ids, v.CollectID)
			}
			err = l.svcCtx.UserCollect.UpdateBatch(l.ctx, nil, ids, model.M{
				model.ForumUserCollectColumns.Status: int8(globalkey.StatusDeleted),
			})
			if err != nil {
				return nil, errorhandler.DefaultRepositoryError(l.Logger, err, in)
			}
		}
	} else if in.Ids != nil {
		if cids := modelx.UniqueInterfaces(strings.TrimSpace(*in.Ids), 64); len(cids) > 0 {
			filterMap := make(map[string]map[string][]any)
			filterMap[model.ForumUserCollectColumns.UserID] = map[string][]any{
				"equalTo": []any{userId},
			}
			filterMap[model.ForumUserCollectColumns.Status] = map[string][]any{
				"equalTo": []any{globalkey.StatusEnabled},
			}
			filterMap[model.ForumUserCollectColumns.CollectID] = map[string][]any{
				"in": cids,
			}
			list, _, err := l.svcCtx.UserCollect.GetList(l.ctx, model.M{}, -1, filterMap)
			if err != nil {
				return nil, errorhandler.DefaultRepositoryError(l.Logger, err, in)
			}

			if len(list) > 0 {
				var ids []uint64
				for _, v := range list {
					ids = append(ids, v.CollectID)
				}
				err = l.svcCtx.UserCollect.UpdateBatch(l.ctx, nil, ids, model.M{
					model.ForumUserCollectColumns.Status:    uint8(globalkey.StatusDeleted),
					model.ForumUserCollectColumns.UpdatedAt: time.Now(),
				})
				if err != nil {
					return nil, errorhandler.DefaultRepositoryError(l.Logger, err, in)
				}
			}
		}
	} else if in.ObjectIds != nil && in.ObjectType != nil {
		if oids := modelx.UniqueInterfaces(strings.TrimSpace(*in.ObjectIds), 64); len(oids) > 0 {
			filterMap := make(map[string]map[string][]any)
			filterMap[model.ForumUserCollectColumns.UserID] = map[string][]any{
				"equalTo": []any{userId},
			}
			filterMap[model.ForumUserCollectColumns.Status] = map[string][]any{
				"equalTo": []any{globalkey.StatusEnabled},
			}
			filterMap[model.ForumUserCollectColumns.ObjectID] = map[string][]any{
				"in": oids,
			}
			filterMap[model.ForumUserCollectColumns.ObjectType] = map[string][]any{
				"equalTo": []any{*in.ObjectType},
			}
			list, _, err := l.svcCtx.UserCollect.GetList(l.ctx, model.M{}, -1, filterMap)
			if err != nil {
				return nil, errorhandler.DefaultRepositoryError(l.Logger, err, in)
			}

			if len(list) > 0 {
				var ids []uint64
				for _, v := range list {
					ids = append(ids, v.CollectID)
				}
				err = l.svcCtx.UserCollect.UpdateBatch(l.ctx, nil, ids, model.M{
					model.ForumUserCollectColumns.Status:    uint8(globalkey.StatusDeleted),
					model.ForumUserCollectColumns.UpdatedAt: time.Now(),
				})
				if err != nil {
					return nil, errorhandler.DefaultRepositoryError(l.Logger, err, in)
				}
			}
		}
	}

	return &forum.BaseResp{
		Msg: i18n.DeleteSuccess,
	}, nil
}
